<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class MaterialList extends Model
{
    protected $table = 'material_list';
    public function goods()
    {
        return $this->belongsTo('App\Model\Goods','goods_id');
    }
    public function stock()
    {
        return $this->hasMany('App\Model\Stock','material_id');
    }
    /**
     * 入库出库归还更新物资名称表
     * type:入库类型1:新增入库；3：归还入库;4:出库
     * @param $goods_id
     * @param $material_id
     * @param $checkcategory_id
     * @param $company_id
     * @return void
     */
    public static function operate($material_id=0){
        $data = Stock::select(DB::raw('SUM(`income_warehouse`) as income_warehouse,SUM(`leave_warehouse`) as leave_warehouse,SUM(`loss_warehouse`) as loss_warehouse,SUM(`warehouse`) as warehouse'))->where('material_id',$material_id);
        $query = $data->first();

        $update_order['income_warehouse']=$query['income_warehouse'];
        $update_order['leave_warehouse']=$query['leave_warehouse'];
        $update_order['loss_warehouse']=$query['loss_warehouse'];
        $update_order['warehouse']=$query['warehouse'];

        $res = self::where('id', $material_id)->update($update_order);
        return $res;
    }
}
